const globalIp = getApp().globalData.userIp
var startPoint
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    //按钮位置参数
    buttonTop: 0,
    buttonLeft: 0,
    windowHeight: '',
    windowWidth: '',
    showModal: false,
    roleName: ''
  },

  openPopup: function() {
    this.setData({
      showModal: true
    })
  },
  
  exit: function() { 
    this.setData({
      showModal: false
    })
  },

  getRoleName: function(e) {
    this.setData({
      roleName: e.detail.value
    })
  },

  trueDel: function(e) {
    let that = this;
    wx.showModal({
      content: '确定要删除吗？(一旦删除，无法恢复)',
      success: function (sm) {
        if (sm.confirm) {
          let doc = e.currentTarget.dataset.doc;
          wx.request({
            url: globalIp + 'home/updateRoleMsg',
            data: {
              openId: wx.getStorageSync('userInfo').openid || '',
              value: 3,
              docEntry: doc
            },
            header: {
              'content-type': 'application/json'
            },
            method: 'POST',
            success: function(res) {
              if (res.data.success) {
                wx.showToast({
                  title: '删除角色成功！',
                  icon: 'success',
                  success: function(res) {
                    setTimeout(() => {
                      that.onShow();
                    }, 1000)
                  }
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            },
            fail: function(res) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })          
  },

  // 提交
  submit: function() {
    let that = this;
    let roleName = that.data.roleName;
    if (roleName == '' || getApp().isNull(roleName)) {
      wx.showModal({
        title: '提示',
        content: '角色名称不能为空!',
        showCancel: false
      })
      return false;
    }
    wx.request({
      url: globalIp + 'home/updateRoleMsg',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
        value: 1,
        userName: roleName
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          wx.showToast({
            title: '创建角色成功！',
            icon: 'success',
            success: function(res) {
              that.setData({
                showModal: false
              })
              setTimeout(() => {
                that.onShow();
              }, 1000)
            }
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },  

  trueIndex: function(e){
    wx.navigateTo({
      url: './examineroleindex/examineroleindex?DocEntry='+e.currentTarget.dataset.num,
    })
  },

  //以下是按钮拖动事件
  buttonStart: function (e) {
    startPoint = e.touches[0]//获取拖动开始点
  },
  buttonMove: function (e) {
    var endPoint = e.touches[e.touches.length - 1]//获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint//重置开始位置
    var buttonTop = this.data.buttonTop + translateY
    var buttonLeft = this.data.buttonLeft + translateX
    //判断是移动否超出屏幕
    if (buttonLeft+50 >= this.data.windowWidth){
      buttonLeft = this.data.windowWidth-50;
    }
    if (buttonLeft<=0){
      buttonLeft=0;
    }
    if (buttonTop<=0){
      buttonTop=0
    }
    if (buttonTop + 50 >= this.data.windowHeight){
      buttonTop = this.data.windowHeight-50;
    }
    this.setData({
      buttonTop: buttonTop,
      buttonLeft: buttonLeft
    })
  },

  confirm : function() {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: globalIp + 'home/showAllRole',
      data: {
        value: '1',
        openId: wx.getStorageSync('userInfo').openid || ''
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          var data = res.data.JSONObject.data;
          if (data.length > 0) {
            that.setData({
              list: data
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {
        wx.hideLoading()
      }
    })
    wx.getSystemInfo({
      success: function (res) {
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          buttonTop:res.windowHeight*0.60,//这里定义按钮的初始位置
          buttonLeft:res.windowWidth*0.80,//这里定义按钮的初始位置
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.confirm();
  },

})